<?php

class MemberController extends AdminController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function actionList() {
        $model = new sys_member('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['sys_member']))
            $model->attributes = $_GET['sys_member'];

        $this->render('list', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);

        if (isset($_POST['sys_member'])) {
            $model->attributes = $_POST['sys_member'];
            if ($model->save())
                $this->redirect(array('update', 'id' => $model->member_network_id));
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    public function actionPassword() {

        $network_mid = function_lib::uri_segment(5);
        $network_id = function_lib::get_network_id($network_mid);

        if ($network_id != false) {
            if (isset($_POST['new_password'])) {
                if ($_POST['new_password'] != '') {
                    if ($_POST['new_password'] === $_POST['confirm_password']) {
                        $transaction = Yii::app()->db->beginTransaction();
                        try {
                            $sql = "UPDATE sys_password, sys_network 
                                SET password_value = MD5('" . $_POST['new_password'] . "') 
                                WHERE network_id = password_network_id AND network_group_network_id = '" . $network_id . "'";
                            Yii::app()->db->createCommand($sql)->execute();
                            $transaction->commit();

                            Yii::app()->user->setFlash('success', 'Data has been saved successfully.');
                        } catch (CDbException $e) {
                            $transaction->rollback();
                            Yii::app()->user->setFlash('error', 'Data failed to save.');
                        }
                    } else {
                        Yii::app()->user->setFlash('error', 'Sorry, The Password that you entered do not match.');
                    }
                } else {
                    Yii::app()->user->setFlash('error', 'Sorry, you have to fill New Password.');
                }
                Yii::app()->controller->redirect(array('/admin/system/member/password/' . $network_mid));
            }

            $this->render('password', array(
                'network_id' => $network_id,
                'network_mid' => $network_mid,
            ));
        } else {
            Yii::app()->controller->redirect(array('/admin/system/member/list'));
        }
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
            'upgrade'=> $this->upgrade($id),
        ));
    }
    
    public function actionUpgrade(){
       $this->render('view', array(
            'model' => $this->loadModel($id),
        )); 
    }

//    public function actionReport() {
//        $this->layout = '//layouts/empty';
//        $mid = function_lib::uri_segment(5);
//        
//        $mid = 1;
//        $summary = vo_network::get_bonus_acc($mid);
//        $today = vo_network::get_bonus_by_netgrow($mid, date('Y-m-d'));
//        $ewallet_income = vo_network::get_ewallet_income_by_date($mid, date('Y-m-d'));
//        $ewallet_cash = $ewallet_noncash = $ewallet_products = $ewallet_voucher = array(
//            'sponsor' => 0,
//            'extra_sponsor' => 0,
//            'gen_node' => 0,
//            'match' => 0,
//            'gen_match' => 0,
//            'matrix_node' => 0,
//            'board_level' => 0,
//            'board_fly' => 0,
//            'total_bonus' => 0,
//        );
//
//        if ($ewallet_income != false) {
//            foreach ($ewallet_income as $row_ewallet_income) {
//                $ewallet_cash[$row_ewallet_income['ewallet_income_bonus_name']] = $row_ewallet_income['ewallet_income_cash'];
//                $ewallet_noncash[$row_ewallet_income['ewallet_income_bonus_name']] = $row_ewallet_income['ewallet_income_noncash'];
//                $ewallet_products[$row_ewallet_income['ewallet_income_bonus_name']] = $row_ewallet_income['ewallet_income_products'];
//                $ewallet_voucher[$row_ewallet_income['ewallet_income_bonus_name']] = $row_ewallet_income['ewallet_income_voucher'];
//
//                $ewallet_cash['total_bonus'] += $row_ewallet_income['ewallet_income_cash'];
//                $ewallet_noncash['total_bonus'] += $row_ewallet_income['ewallet_income_noncash'];
//                $ewallet_products['total_bonus'] += $row_ewallet_income['ewallet_income_products'];
//                $ewallet_voucher['total_bonus'] += $row_ewallet_income['ewallet_income_voucher'];
//            }
//        }
//
//        $this->render('report', array(
//            'summary' => $summary,
//            'today' => $today,
//            'ewallet_cash' => $ewallet_cash,
//            'ewallet_noncash' => $ewallet_noncash,
//            'ewallet_products' => $ewallet_products,
//            'ewallet_voucher' => $ewallet_voucher,
//        ));
//    }

    public function actionReport()
    {
        
        $this->layout = '//layouts/empty';
        $network_id = str_replace('.', '', function_lib::uri_segment(5)); 
        $date_val = str_replace('.', '', function_lib::uri_segment(6)); 
        $bonus= new Bonus_helper;
        //if(!isset($_POST['date'],$_POST['network_id']))
       // {
             
              

            //summary bonus
            $summary=$bonus->summary_bonus($network_id);

            //statement
            $statement=$bonus->statement_bonus($network_id);

            //history withdraw
            $history_withdraw=$bonus->history_withdraw($network_id);

            //history upgrade
            $history_upgrade=$bonus->history_upgrade($network_id,$merge=true);
             $this->render(
                'report',array(
                   'summary'=>$summary,
                    'network_id'=>$network_id,
                    'statement'=>$statement,
                    'history_withdraw'=>$history_withdraw,
                    'history_upgrade'=>$history_upgrade,
                    'bonus'=>$bonus,
                    //'bonus'=>$bonus,
                 //   'value'=>$value,
                ));
      //  }
       
       
    
//     if(isset($_POST['date'],$_POST['network_id']))
//        {
////          $statement=$bonus->statement_bonus($_POST['network_id']);
////            $this->renderPartial('statement',array(
////            //'statement' => $statement,
////            'date_start' => $statement['date_start'],
////            'date_end' => $statement['date_end'],
////            'date_diff' => $statement['date_diff'],
////            'data' => $statement['data'],
////            'total' => $statement['total'],
////        ));
//         
//            $statement_detail=$bonus->statement_detail($_POST['network_id'], $_POST['date']);
//            
//            $this->renderPartial('statement_detail', array(
//            'data' => $statement_detail['data'],
//            'percent' => $statement_detail['percent'],
//            'bonus' => $statement_detail['bonus'],
//            'cut_index' => $statement_detail['cut_index'],
//            'cut_adm' => $statement_detail['cut_adm'],
//            'subtotal' => $statement_detail['subtotal'],
//            'total' => $statement_detail['total'],
//        ));
//        } 
        
       
    }
    
    public function actionAutologin() {
        $username = function_lib::uri_segment(5);

        Yii::app()->controller->redirect(array('/member/login/auto/' . $username));
    }

    public function loadModel($id) {
        $model = sys_member::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }
    
    public function upgrade($id){
        //$sql="SELECT * FROM sys_serial_products_user LEFT JOIN sys_network ON sys_serial_products_user.serial_products_user_network_id=sys_network.network_id WHERE sys_serial_products_user.serial_products_user_network_id='8114' ORDER BY sys_serial_products_user.serial_products_user_datetime";
       //$upgrade = Yii::app()->db->createCommand($sql)->queryRow();
        
       $upgrade = sys_serial_products_user::model()->findByPk($id);
        //$sql="SELECT serial_products_user_serial_products_id as serial_upgrade, serial_products_user_datetime as tanggal FROM sys_serial_products_user LEFT JOIN sys_network ON sys_serial_products_user.serial_products_user_network_id=sys_network.network_id WHERE sys_serial_products_user.serial_products_user_network_id=$id ORDER BY sys_serial_products_user.serial_products_user_datetime";
        //$upgrade =Yii::app()->db->createCommand($sql)->queryAll();
        $upgrade = Yii::app()->db->createCommand()
    ->select('*')
    ->from('sys_serial_products_user u')
    ->join('sys_network p', 'u.serial_products_user_network_id=p.network_id')
    ->join('ecom_products_category c', 'c.products_category_prefix=left(u.serial_products_user_serial_products_id,4)')
    ->where('serial_products_user_network_id=:serial_products_user_network_id', array(':serial_products_user_network_id'=>$id))
    ->queryAll();
        return $upgrade;
    }

}
